package com.gree.flowable.app.listener.task;

import com.gree.flowable.app.listener.BaseListener;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.SequenceFlow;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 流程并行节点分配人，这监听器一般放在流程线上
 */
@Component(value = "setPallelAssigneeListener")
public class SetPallelAssigneeListener extends BaseListener implements ExecutionListener {

    @Override
    public void notify(DelegateExecution execution) {
        FlowElement element = execution.getCurrentFlowElement();
        String target = ((SequenceFlow) element).getTargetRef();
        Object assignees = execution.getVariable(target + "_assignee");
        if (assignees == null || assignees.toString().length() == 0){
            throw new RuntimeException("没有设置候选人！");
        }
        String[] assigneesArray = assignees.toString().split(",");
        execution.setVariable("assigneeList", Arrays.asList(assigneesArray));
    }
}
